$(function () {
    var sjdj = {
        grid: $("#grid_sjdj"),
        gridOptions: {
            url: $.webPath() + "/kcgl/sjdj/sjdjList",
            toolbar: '#toolbar_sjdj',
            fitColumns: false,
            singleSelect: false,
            pagination: true,
            checkbox: true,
            fit: true,
            pageNumber: 1,
            rownumbers: true,
            columns: [
                [
                    {field: 'id', checkbox: true},
                    {field: 'htbm', title: '合同编码', width: 120},
                    {field: 'qdrq', title: '签订日期', width: 100},
                    {field: 'gname', title: '品名', width: 100},
                    {field: 'gcz', title: '材质', width: 100},
                    {field: 'ggxh', title: '规格型号', width: 100},
                    {field: 'gdw', title: '单位', width: 50},
                    {field: 'xsdh', title: "销售系统子项号", width: 100},
                    {field: 'carno', title: "车皮号", width: 100},
                    {field: 'fhrq', title: "发货日期", width: 100},
                    {field: 'zzl', title: "总重量", width: 80},
                    {field: 'sjdj', title: "实际单价",
                        formatter: function (value) {
                            var val = formatJeForHtml2(value);
                            return val;
                        }, width: 80},
                    {field: 'zt', title: "状态",
                        formatter: function (value) {
                            if (value == 0) {
                                return "已保存";
                            }
                            if (value == 1) {
                                return "已审核";
                            }
                            if (value == 2) {
                                return "审核未通过";
                            }
                        }, width: 100},
                    {field: 'cxm', title: "创建人", width: 80},
                    {field: 'crq', title: "创建日期", width: 80},
                    {field: 'shr', title: "审核人", width: 80},
                    {field: 'shrq', title: "审核日期", width: 80}
                ]
            ]
        }
    }
    sjdj.grid.edatagrid(sjdj.gridOptions);

    //查询按钮
    $("#btn_sjdj_search").click(function () {
        sjdj_search();
    });


    //批量入账
    $("#btn_sjdj_pldr").click(function () {
        var win = $.window({
            title: "批量导入",
            width: 450,
            height: 240,
            cache: false,
            url: $.webPath() + "/kcgl/sjdj/sjdj_upload",
            modal: true,
            buttons: [
                {
                    text: '确认',
                    handler: function () {
                        $('#sjdj_uploadSubject').form('submit', {
                            onSubmit: function () {
                                $(win).mask();
                            },
                            success: function (data) {
                                $(win).mask('hide');
                                var json = $.parseJSON(data);
                                if (json.statusCode == 200) {
                                    $(win).window('close');
                                    $('#grid_sjdj').datagrid('reload');
                                    $.messager.show({
                                        title: '提示',
                                        msg: json.message,
                                        timeout: 3000,
                                        showType: 'slide'
                                    });
                                } else
                                    $.messager.alert('提示', json.message, 'error');
                            }
                        });
                    }
                }
            ]
        });
    });

    //下载模板
    $("#btn_sjdj_xzmb").click(function () {
        window.open($.webPath() + "/kcgl/sjdj/sjdj_title_export");
    });

});

function sjdj_search() {
    var zt = $("#toolbar_sjdj input[name='sjdj_zt']").val();
    $('#grid_sjdj').datagrid('load',
        {
            htbm: $('#cght_htbm').val(),
            carno: $('#carno').val(),
            fhrq: $('#fhrq').datebox('getValue'),
            xsdh: $('#xsdh').val(),
            zt: zt
        }
    );
}

//批量审核
var sjdj_win;
function sjdj_confirm() {
    var rows = $("#grid_sjdj").datagrid("getChecked");
    var sjdjIds = "";
    if (!rows || rows.length == 0) {
        $.messager.alert("提示", "请勾选要审核的记录!", "info");
        return false;
    } else {
        $.each(rows, function (i, val) {
            sjdjIds += val["id"] + ",";
        });
    }
    var href = $.webPath() + "/kcgl/sjdj/sjdjsh";
    var button = [
        {text: '审核通过', handler: function () {
            sjdjConfrimToAction(sjdjIds, "yes");
        }},
        {text: '驳回', handler: function () {
            sjdjConfrimToAction(sjdjIds, "no");
        }}
    ];
    sjdj_win = $.window({
        title: '审核实际单价',
        iconCls: 'icon-add2',
        url: href,
        buttons: button,
        width: 300,
        height: 200,
        model: true,
        maximizable: true,
        maximized: true,
        isMax: false
    });
}

function sjdjConfrimToAction(sjdjIds, shzt) {
    var actionUrl = $.webPath() + "/kcgl/sjdj/sjdj_confirm";
    $.post(actionUrl, {sjdjIds: sjdjIds, shzt: shzt}, function (data) {
        var obj = eval('(' + data + ')');
        $(sjdj_win).window('close');
        $('#grid_sjdj').datagrid('reload');
        $.messager.show({title: '提示框', msg: obj.message, timeout: 3000, showType: 'slide'});
    })
}
